import threading
import tkinter as tk
from tkinter import ttk

from module.part1Module import part1_process_data
from module.part21Module import part21_process_data
from ui.ProgressBarWindow import ProgressBarWindow
from ui.SelectExcelFileSheet import SelectExcelFileSheet
from module.part22Module import part22_process_data
from module.part23Module import part23_process_data

class MainWindow:
    def __init__(self):
        self.root = None
        self.window_width = 900
        self.window_height = 700
        self.screen_width = 0
        self.screen_height = 0
        self.x = (self.screen_width - self.window_width) // 2
        self.y = (self.screen_height - self.window_height) // 2
        self.btn4Statistics4Week: tk.Button = None
        self.btn4StatisticsIdleAccounts1: tk.Button = None
        self.btn4StatisticsIdleAccounts2: tk.Button = None
        self.btn4StatisticsIdleAccounts3: tk.Button = None
        self.lable4TaskState: tk.Label = None
        self.progress_bar:ttk.Progressbar = None
        self.frame1: tk.Frame = None
        #self.progressBarWindow: ProgressBarWindow = None
    def init2(self):
        self.window_width = 900
        self.window_height = 700
        if self.root is not None:
          self.screen_width = self.root.winfo_screenwidth()
          self.screen_height = self.root.winfo_screenheight()
        self.x = (self.screen_width - self.window_width) // 2
        self.y = (self.screen_height - self.window_height) // 2
        self.btn4Statistics4Week: tk.Button = None
        self.btn4StatisticsIdleAccounts1: tk.Button = None
        self.btn4StatisticsIdleAccounts2: tk.Button = None
        self.btn4StatisticsIdleAccounts3: tk.Button = None
        self.lable4TaskState: tk.Label = None
        self.progress_bar: ttk.Progressbar = None
        self.frame1: tk.Frame = None
        #self.progressBarWindow: ProgressBarWindow = None

    def on_btn4Statistics4Week_click(self):
        self.stop_progress()
        threading.Thread(target=self.run_part1_process_data).start()
    def run_part1_process_data(self):
        try:
            selectExcelFileSheet = SelectExcelFileSheet(self.root)
            part1_process_data(selectExcelFileSheet)
        finally:
            self.stop_progress()  # 在主线程中调用 stop_progress 方法

    def start_progress(self):
        self.progress_bar.start()
        self.lable4TaskState.config(text="任务正在执行，请按照提示操作，请不要关闭界面...")
    def stop_progress(self):
        self.progress_bar.stop()
        self.lable4TaskState.config(text="任务执行完成")
    def on_btn4StatisticsIdleAccounts1_click(self):
        self.start_progress()
        threading.Thread(target=self.run_part21_process_data).start()

    def run_part21_process_data(self):
        try:
            selectExcelFileSheet = SelectExcelFileSheet(self.root)
            part21_process_data(selectExcelFileSheet)
        finally:
            self.stop_progress()  # 在主线程中调用 stop_progress 方法

    def on_btn4StatisticsIdleAccounts2_click(self):
        self.start_progress()
        # 启动一个线程来执行 part21_process_data 方法
        threading.Thread(target=self.run_part22_process_data).start()

    def run_part22_process_data(self):
        try:
            selectExcelFileSheet = SelectExcelFileSheet(self.root)
            part22_process_data(selectExcelFileSheet)
        finally:
            self.stop_progress()  # 在主线程中调用 stop_progress 方法

    def on_btn4StatisticsIdleAccounts3_click(self):
        self.start_progress()
        threading.Thread(target=self.run_part23_process_data).start()

    def run_part23_process_data(self):
        try:
            selectExcelFileSheet = SelectExcelFileSheet(self.root)
            part23_process_data(selectExcelFileSheet)
        finally:
            self.stop_progress()  # 在主线程中调用 stop_progress 方法
    def showWindow(self):
        self.root = tk.Tk()
        self.root.title("Excel 表格统计小工具")
        self.init2()
        self.root.geometry(f"{self.window_width}x{self.window_height}+{self.x}+{self.y}")

        self.frame1 = tk.Frame(self.root)
        self.frame1.pack(anchor='center', pady=5)
        self.lable4TaskState = tk.Label(self.frame1, text="任务状态：")
        self.lable4TaskState.pack(side=tk.LEFT, padx=10, pady=10)
        self.progress_bar = ttk.Progressbar(self.frame1,  orient="horizontal", length=300, mode="indeterminate")
        self.progress_bar.pack(side=tk.LEFT, padx=10, pady=10)

        self.btn4Statistics4Week = tk.Button(self.root, text="1.闲置周报表格逻辑关系", command=self.on_btn4Statistics4Week_click)
        self.btn4Statistics4Week.pack(anchor='center', pady=5)  # 添加按钮到窗口，并设置垂直间距
        # 禁用按钮
        self.btn4Statistics4Week.config(state=tk.DISABLED)

        self.btn4StatisticsIdleAccounts1 = tk.Button(self.root, text="5.闲置台账表格逻辑关系--成果表1.清单",
                                               command=self.on_btn4StatisticsIdleAccounts1_click)
        self.btn4StatisticsIdleAccounts1.pack(anchor='center', pady=5)  # 添加按钮到窗口，并设置垂直间距

        self.btn4StatisticsIdleAccounts2 = tk.Button(self.root, text="5.闲置台账表格逻辑关系--成果表2.全省剩余闲置（含预增）",
                                                     command=self.on_btn4StatisticsIdleAccounts2_click)
        self.btn4StatisticsIdleAccounts2.pack(anchor='center', pady=5)  # 添加按钮到窗口，并设置垂直间距

        self.btn4StatisticsIdleAccounts3 = tk.Button(self.root, text="5.闲置台账表格逻辑关系--成果表3.全省可处置未处置情况表",
                                                     command=self.on_btn4StatisticsIdleAccounts3_click)
        self.btn4StatisticsIdleAccounts3.pack(anchor='center', pady=5)  # 添加按钮到窗口，并设置垂直间距

        self.root.mainloop()
